package com.ruoyi.report.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedData.service.IAttachedDataService;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.engineering.domain.Engineering;
import com.ruoyi.engineering.service.IEngineeringService;
import com.ruoyi.report.domain.Report;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.task.domain.Task;
import com.ruoyi.task.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.report.mapper.ReportMapper;
import com.ruoyi.report.service.IReportService;

/**
 * 检测报告Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class ReportServiceImpl implements IReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISampleService sampleService;

    @Autowired
    private IAttachedDataService attachedDataService;
    @Autowired
    private TaskMapper taskMapper;

    /**
     * 查询检测报告
     *
     * @param id 检测报告主键
     * @return 检测报告
     */
    @Override
    public Report selectReportById(Long id) {
        return reportMapper.selectReportById(id);
    }

    /**
     * 查询检测报告列表
     *
     * @param report 检测报告
     * @return 检测报告
     */
    @Override
    public List<Report> selectReportList(Report report) {
        List<Report> list = reportMapper.selectReportList(report);
        if (!list.isEmpty()) {
            for (Report r : list) {
                if ("shinei".equals(r.getCategory())) {
                    Sample sampleOne = sampleService.selectSampleById(r.getSampleId());

                    r.setChargePerson(sampleOne.getUserName() + "(" + sampleOne.getNickName() + ")");
                }
                if ("shiwai".equals(r.getCategory())) {
                    Long attachedDataId = Long.parseLong(r.getAttachedDataId());
                    AttachedData attachedDataOne = attachedDataService.selectAttachedDataById(attachedDataId);
                    r.setChargePerson(attachedDataOne.getUserName() + "(" + attachedDataOne.getNickName() + ")");
                }
            }
        }
        return list;
    }

    /**
     * 新增检测报告
     *
     * @param report 检测报告
     * @return 结果
     */
    @Override
    public int insertReport(Report report) {
        report.setCreateTime(DateUtils.getNowDate());
        return reportMapper.insertReport(report);
    }


    @Override
    public Long createReportNumber(Report report) {
        report.setCreateTime(DateUtils.getNowDate());
        Integer res = reportMapper.insertReport(report);

        Long id = report.getId();
        //生成报告编号  工程编号➕分类编码➕年份➕序号
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR); // 获取当前年份

        Long commissionId = report.getCommissionId();
        Commission commissionOne = commissionService.selectCommissionById(commissionId);

        Engineering engineeringOne = engineeringService.selectEngineeringById(commissionOne.getEngineeringId());

        //分类编码
        String fenleiNumber = "";
        String projectOld = report.getProjectOld();
        if ("shinei".equals(report.getBusinessType())) {
            List<SysDictData> sysDictDatalist = dictDataService.selectDictDataByDictType("detection_item_indoor");
            for (SysDictData sysDictData : sysDictDatalist) {
                if (sysDictData.getDictValue().equals(projectOld)) {
                    fenleiNumber = sysDictData.getCssClass();
                }
            }
        }

        if ("shiwai".equals(report.getBusinessType())) {
            List<SysDictData> sysDictDatalist = dictDataService.selectDictDataByDictType("detection_item_scene");
            for (SysDictData sysDictData : sysDictDatalist) {
                if (sysDictData.getDictLabel().equals(projectOld)) {
                    fenleiNumber = sysDictData.getDictValue();
                }
            }
        }
        String number = engineeringOne.getEngineeringNumber() + fenleiNumber + year + id;

        Report report2 = new Report();
        report2.setEngineeringId(commissionOne.getEngineeringId());//工程id
        report2.setId(id);
        report2.setReportNumber(number);
        reportMapper.updateReport(report2);

        return id;
    }

    /**
     * 修改检测报告
     *
     * @param report 检测报告
     * @return 结果
     */
    @Override
    public int updateReport(Report report) {
        report.setUpdateTime(DateUtils.getNowDate());
        if(null == report.getCommissionId()){
            Commission commission = commissionService.selectCommissionById(report.getCommissionId());
            Task task1 = new Task();
            task1.setTaskType("3");
            task1.setCommissionId(commission.getId());
            List<Task> taskList = taskMapper.selectTaskList(task1);
            if (!taskList.isEmpty()) {
                task1.setId(taskList.get(0).getId());
                task1.setStatus("1");
                task1.setExecuteTime(DateUtils.getNowDate());
                taskMapper.updateTask(task1);
            }
        }
        return reportMapper.updateReport(report);
    }

    /**
     * 批量删除检测报告
     *
     * @param ids 需要删除的检测报告主键
     * @return 结果
     */
    @Override
    public int deleteReportByIds(Long[] ids) {
        return reportMapper.deleteReportByIds(ids);
    }


    /**
     * 批量逻辑删除检测报告
     *
     * @param ids 需要删除的检测报告主键
     * @return 结果
     */
    @Override
    public int logicDeleteReport(Long[] ids, String delType, Long delUser) {
        return reportMapper.logicDeleteReport(ids, delType, delUser);
    }

    /**
     * 删除检测报告信息
     *
     * @param id 检测报告主键
     * @return 结果
     */
    @Override
    public int deleteReportById(Long id) {
        return reportMapper.deleteReportById(id);
    }
}
